#coding: utf-8
from django.http import HttpResponse
import json
from .models import ServerMenu,ServerInfo
from django.forms.models import model_to_dict
def getList(request):
    list_count = ServerMenu.objects.count()
    list1 = []
    for obj in ServerMenu.objects.all():
        pid = obj.pid
        text = obj.text
        remark = obj.remark
        state = obj.state
        iconCls = obj.iconCls
        url = obj.url
        dict1 = {'id':obj.pk,'pid' : pid,'text': text,'remark': remark,'state': state,'iconCls': iconCls,'url':url}
        list1.append(dict1)
    result = {"total": list_count, 'rows': list1}
    return HttpResponse(json.dumps(result))



def addList(request):
    ret = {'state': 200, 'message': "添加成功"}
    data = json.loads(request.body)
    print data
    ServerMenu.objects.create(**data)
    return HttpResponse(json.dumps(ret), content_type="application/json")


def  modifyList(request):
    ret = {'state': 200, 'message': "修改成功"}
    data = json.loads(request.body)
    print data
    number = data.pop('id')
    ServerMenu.objects.filter(id=number).update(**data)
    return HttpResponse(json.dumps(ret), content_type="application/json")

def serverList(request):
    get_id = request.POST.get('id',None)

    if not get_id:
        get_id = 0
        ret = {'state': 200, 'message': "添加成功"}
        data1 = ServerMenu.objects.filter(pid=get_id)

        data = [model_to_dict(i) for i in data1]
        print data

        return HttpResponse(json.dumps(data), content_type="application/json")

    else:
        ret = {'state': 200, 'message': "添加成功"}
        data1 = ServerMenu.objects.filter(pid=get_id)

        data = [model_to_dict(i) for i in data1]
        print data

        return HttpResponse(json.dumps(data), content_type="application/json")
def getServerList(request):
    get_id = request.GET.get('id', None)
    print get_id
    list1 = []
    data1 = ServerInfo.objects.filter(servermenu_id=get_id)
    count = data1.count()
    for obj in data1:
        servermenu_id = obj.servermenu_id
        host_name = obj.host_name
        ip = obj.ip
        id = obj.id
        port = obj.port
        system = obj.system
        user = obj.user
        password = obj.password
        remark = obj.remark
        dict1 = { 'host_name': host_name,'ip': ip,'port': port,'system': system,'user': user,'password':password,'remark': remark,'id': id}
        list1.append(dict1)
    result = {"total": count, 'rows': list1}
    print result
    return HttpResponse(json.dumps(result), content_type="application/json")
def addServerList(request):
    ret = {'state': 200, 'message': "添加成功"}
    data = json.loads(request.body)

    servermenu_id = data.pop('servermenu_id')
    obj = ServerMenu.objects.get(id = servermenu_id)
    data['servermenu_id'] = obj
    print data
    ServerInfo.objects.create(**data)
    return HttpResponse(json.dumps(ret), content_type="application/json")

def modifyServerList(request):
    ret = {'state': 200, 'message': "修改成功"}
    print request.body
    data = json.loads(request.body)
    id = data.pop('id')
    print data
    ServerInfo.objects.filter(id=id).update(**data)
    return HttpResponse(json.dumps(ret), content_type="application/json")

   # return HttpResponse(json.dumps(ret), content_type="application/json")

# #
#
#     data['RootCert_id'] = obj

#     return HttpResponse(json.dumps(ret), content_type="application/json")




